#coding=utf8
from __future__ import division
from torch import nn
import torch
import torch.utils.data as torchdata
from torchvision import datasets,transforms
import os,time
import pandas as pd
import numpy as np
from sklearn.metrics import f1_score, precision_recall_fscore_support, classification_report,confusion_matrix
from glob import glob
import cPickle
import itertools
from sklearn.model_selection import train_test_split
import os
import numpy as np
import pandas as pd
import zipfile
from utils.metrics import cal_csv_mAP
from sklearn.model_selection import train_test_split
from FSdata.FSdataset import FSdata, collate_fn,attr2length_map, idx2attr_map,attr2idx_map


def str2np(x):
    return np.array(x.split(';')).astype(np.float)

def np2str(arr):
    return ';'.join(['%.10f'%x for x in arr])

def np2str2(arr):
    return ';'.join(['%.4f'%x for x in arr])




val_pd = pd.read_csv('./online_pred1b/csv/fashionAI_attributes_answer_b_20180428.csv',
                     header=None, names=['ImageName', 'AttrKey', 'AttrValues'])
# 985517
csv_root = './val_pred2/part_csv'
files = ['NASnet_r2_9625(dpred2).csv']

bset_score = 0
best_comb = []
for comb_num in range(len(files),len(files)+1):
    print comb_num, '/', len(files)
    for comb in itertools.combinations(files,comb_num):
        scores = []
        file_path = os.path.join(csv_root, comb[0])
        merged_pd = pd.read_csv(file_path, header=None, names=['ImageName', 'AttrKey', 'AttrValueProbs'])
        merged_pd['AttrValueProbs'] = merged_pd['AttrValueProbs'].apply(str2np)

        for file_name in comb[1:]:
            file_path = os.path.join(csv_root,file_name)
            part_pd = pd.read_csv(file_path, header=None, names=['ImageName', 'AttrKey', 'AttrValueProbs'])
            merged_pd['AttrValueProbs'] += part_pd['AttrValueProbs'].apply(str2np)

        merged_pd['AttrValueProbs'] = merged_pd['AttrValueProbs'] / len(comb)
        merged_pd['AttrValueProbs'] = merged_pd['AttrValueProbs'].apply(np2str)
        merged_pd['AttrValueProbs'] = merged_pd['AttrValueProbs'].apply(str2np)
        val_mAP, APs, accs = cal_csv_mAP(merged_pd, val_pd, idx2attr_map.values())


        if val_mAP > bset_score:
            bset_score  = val_mAP
            best_comb = comb

print len(files), len(best_comb)
for item in best_comb:
    print item
print bset_score